package com.ruoyi.dmp.common.util;

import org.apache.commons.lang3.RandomUtils;

import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;


/**
 * Desc: 字符串工具类
 * Auth: MquanZeng
 * Date: 2020/3/12
 */
public class DmpStringUtil {


    /**
     * scala的main方法参数转为字符串
     *
     * @param args
     * @return
     */
    public static Boolean scalaArgsIsDevEnv(List<String> args) {

        Boolean isDevEnv = false;
        if (args.size() == 0) {
            isDevEnv = true;
        } else {
            for (String item : args) {
                if ("env:dev".equals(item)) {
                    isDevEnv = true;
                }
            }
        }
        return isDevEnv;
    }

    /**
     * scala的main方法参数转为字符串
     *
     * @param args
     * @return
     */
    public static String scalaArgsToString(List<String> args) {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < args.size(); i++) {
            if (i > 0) {
                builder.append(" ");// 用空格分隔
            }
            builder.append(args.get(i));
        }
        return builder.toString();
    }

    /**
     * 获取程序在哪运行的信息
     *
     * @return
     * @throws Exception
     */
    public static String getProgramRunInfo() throws Exception {
        InetAddress addr = InetAddress.getLocalHost();
        String ip = addr.getHostAddress().toString(); // 获取本机ip
        String hostName = addr.getHostName().toString(); // 获取本机计算机名称
        String userName = System.getProperties().getProperty("user.name");// 获取当前用户名
        return ip + "|" + hostName + "|" + userName;
    }

    /**
     * 获取UUID字符串
     *
     * @return
     */
    public static String getUUID() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    /**
     * 指定hdfs文件前缀
     *
     * @param hdfsFilePrefix hdfs文件前缀
     * @return
     */
    public static String getSpecifiHdfsFilePrefixName(String hdfsFilePrefix) {
        StringBuilder builder = new StringBuilder();
        builder.append(hdfsFilePrefix).append("_").append(DmpDateTimeUtil.getCurrentTimeByUnion());
        builder.append("_").append(RandomUtils.nextInt(10000, 90000));
        builder.append(".txt");
        return builder.toString();
    }

    /**
     * 获取随机生成的hdfs文件名
     *
     * @return
     */
    public static String getRandomHdfsFileName() {
        return getSpecifiHdfsFilePrefixName("f");
    }


    /**
     * 获取不包含的内容
     *
     * @param allList 大范围的字符串集合
     * @param subList 小的字符串集合
     * @return
     */
    public static List<String> getNotContain(List<String> allList, List<String> subList) {
        List<String> result = new ArrayList<>();
        for (String item : allList) {
            if (!subList.contains(item)) {
                result.add(item);
            }
        }
        return result;
    }

    /**
     * 截取2个指定字符串之间的字符串
     *
     * @param str   待截取的字符串
     * @param strStart  开始字符串
     * @param strEnd    结束字符串
     * @return
     */
    public static String subMiddleString(String str, String strStart, String strEnd) {
        // 找出指定的2个字符在 该字符串里面的 位置
        int strStartIndex = str.indexOf(strStart);
        int strEndIndex = str.indexOf(strEnd);

        if(strStartIndex == -1 || strEndIndex==-1){
            return "";
        }
        /* 开始截取 */
        return str.substring(strStartIndex, strEndIndex).substring(strStart.length());
    }


    public static void main(String[] args) {
        System.out.println(getRandomHdfsFileName());
        System.out.println(subMiddleString("abcdefg", "nn", "g"));
    }


}
